Response history
The chat history must be passed to the cleaning agent for context. In case of a series of instructions rather than a single one, the responses to the intermediate tasks may be required as context for the subsequent tasks, so also must be passed as history.
history = []
for i in range(len(tasks)):
prompt = cleaned(llm, tasks[i], "\n".join(history)).strip().split("\n") # clean the sub-task with the history as context
for elem in prompt:
if "1. " in elem:
prompt = elem.split("1. ")[1] # extract the cleaned prompt
break
if "(" in prompt:
prompt = prompt.split("(")[0].strip() # the cleaned propt may contain '(prompt was clean already ...)'
response = ask_query(prompt, country)
history.append("human: " + prompt)
history.append("system: " + str(response)) # append the current sub-task and response to the history
This way, the agent processing the prompt always has enough context to answer correctly.